const fs = require('fs-extra') 
const chalk = require('chalk');
const del = (path) => {
  return new Promise((resolve, reject) => {
    fs.remove('./' + path, (err) => {
      if (err) return reject(err)
      resolve(path)
    })
  })

}
function cp(old, path) {
  fs.copy(old, './' + path + old.substring(2), err => {
    if (err) return console.log(err)
    
    console.log(chalk.green(`打包 ${old} success!`))
  })
}
const copy = async (path, env) => {
  let data = await del(path).catch(err => err)
  console.log(chalk.red(`删除 ${data} success!`));
  if (env === "pkg") {
    cp("./view", path)
    cp("./www", path)
    cp("./config.js", path)
  }else{
    cp("./app",path)
    cp("./view", path)
    cp("./www", path)
    cp("./config.js", path)
    cp("./production.js",path)
    cp("./package.json",path)
  }

}

let pkg = false
for (var i of process.argv) {
  if (/pkg/i.test(i)) {
    pkg = true
  }
}

if(pkg){
  copy("dist/", "pkg")
}else{
  copy("dist/", "production")
}


